package a2022.a20220527;

/**
 * @author lenovo
 * @date 2022/5/27
 */
public class a {
    /**
     * 输入：words = ["I","am","MyCalendarThree","student","from","MyCalendarThree","university","in","MyCalendarThree","city"], w
     * ord1 = "MyCalendarThree", word2 = "student"
     * 输出：1
     */
    public static void main(String[] args) {
        String[] wards = {"I","am","MyCalendarThree","student","from","MyCalendarThree","university","in","MyCalendarThree","city"};

        findClosest(wards,"MyCalendarThree","student");
    }
    public static int findClosest(String[] words, String word1, String word2) {
        int n = words.length, ans = n;
        for (int i = 0, p = -1, q = -1; i < n; i++) {
            String t = words[i];
            if (t.equals(word1)) p = i;
            if (t.equals(word2)) q = i;
            if (p != -1 && q != -1) ans = Math.min(ans, Math.abs(p - q));
        }
        return ans;
    }


}
